package com.szsh.aiot.hsm.controller;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.szsh.aiot.common.annotation.CustomPermission;
import com.szsh.aiot.common.annotation.Log;
import com.szsh.aiot.common.core.controller.BaseController;
import com.szsh.aiot.common.core.domain.AjaxResult;
import com.szsh.aiot.common.core.domain.RequestDto;
import com.szsh.aiot.common.enums.BusinessType;
import com.szsh.aiot.common.utils.StringUtils;
import com.szsh.aiot.hsm.service.NetworkProtectService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 网络防护
 */
@RequestMapping(path = {"/app/networkProtect"})
public class NetworkProtectController extends BaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(NetworkProtectController.class);

    private final NetworkProtectService networkProtectService;

    public NetworkProtectController(NetworkProtectService networkProtectService) {
        this.networkProtectService = networkProtectService;
    }

    /**
     * 网络防护开关查询接口
     * @param requestDto 请求参数
     * @return 处理结果
     */
    @RequestMapping(value = "/query")
    @Log(title = "网络防护开关查询接口", businessType = BusinessType.SELECT)
    @CustomPermission
    @ResponseBody
    public AjaxResult query(@RequestBody RequestDto requestDto) {
        return networkProtectService.query(requestDto);
    }

    /**
     * 网络防护开关修改接口
     * @param requestDto 请求参数
     * @return 处理结果
     */
    @RequestMapping(value = "/switchChange")
    @Log(title = "网络防护开关修改接口", businessType = BusinessType.UPDATE)
    @CustomPermission
    @ResponseBody
    public AjaxResult switchChange(@RequestBody RequestDto requestDto) {
        return networkProtectService.switchChange(requestDto);
    }

    /**
     * 网络防护绑定手机号码接口
     * @param requestDto 请求参数
     * @return 处理结果
     */
    @RequestMapping(value = "/bindPhone")
    @Log(title = "网络防护绑定手机号码接口", businessType = BusinessType.UPDATE)
    @CustomPermission
    @ResponseBody
    public AjaxResult bindPhone(@RequestBody RequestDto requestDto) {
        // 验证参数
        JsonObject requestParams = requestDto.getRequestJsonElement().getAsJsonObject();

        // 参数验证
        // phone
        if (!requestParams.has("phone")) {
            if (LOGGER.isErrorEnabled()) {
                LOGGER.error("[phone缺失],requestParams:[" + requestParams + "]");
            }
            return AjaxResult.badRequest("phone缺失");
        }
        JsonElement jsonElement = requestParams.get("phone");
        if (!jsonElement.isJsonPrimitive()) {
            if (LOGGER.isErrorEnabled()) {
                LOGGER.error("[phone格式错误],phone:[" + jsonElement + "]");
            }
            return AjaxResult.badRequest("phone格式错误");
        }
        String phone = jsonElement.getAsString();
        if (StringUtils.isBlank(phone)) {
            if (LOGGER.isErrorEnabled()) {
                LOGGER.error("[phone为空],returnProject:[" + jsonElement + "]");
            }
            return AjaxResult.badRequest("phone为空");
        }

        return networkProtectService.bindPhone(requestDto);
    }

    /**
     * 网络防护解绑手机号码接口
     * @param requestDto 请求参数
     * @return 处理结果
     */
    @RequestMapping(value = "/unBindPhone")
    @Log(title = "网络防护解绑手机号码接口", businessType = BusinessType.UPDATE)
    @CustomPermission
    @ResponseBody
    public AjaxResult unBindPhone(@RequestBody RequestDto requestDto) {
        return networkProtectService.unBindPhone(requestDto);
    }
}
